1445A - Array Rearrangment - CodeForces Solution


greedy sortings *800

Please click on ads to support us..

Python Code:

tt=int(input())
arrow=[]
while tt>0:
    w=input()
    w=w.split()
    n=int(w[0])
    x=int(w[1])
    a=input()
    b=input()
    a=a.split()
    b=b.split()
    a=list(map(int, a))
    b=list(map(int, b))
    a.sort()
    b.sort(reverse= True)
    k=0
    ans='No'
    for i in range(n):
        if a[i]+b[i]>x:
            k+=1
            break
    if k==0:
        ans='Yes'
    arrow.append(ans)
    tt-=1
    if tt!=0:
        r=input()
for i in range(len(arrow)):
    print(arrow[i])

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	  int n,x;
	  cin>>n>>x;
	  int a[n],b[n];
	  for(int i=0; i<n; i++)cin>>a[i];
	   for(int i=0; i<n; i++)cin>>b[i];
	   sort(a,a+n);
	   sort(b,b+n);
	   bool check=true;
	   for(int i=0; i<n; i++){
	       if((a[i]+b[n-1-i])>x){
	           check=false;
	           break;
	       }
	   }
	  
	  if(check)cout<<"Yes"<<endl;
	  else cout<<"No"<<endl;
	  
	 }
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences
1154B - Make Them Equal
1272B - Snow Walking Robot
522B - Photo to Remember
608B - Hamming Distance Sum
1408F - Two Different
274B - Zero Tree
1726H - Mainak and the Bleeding Polygon
722A - Broken Clock
129B - Students and Shoelaces
697B - Barnicle
903D - Almost Difference
1443B - Saving the City
1215C - Swap Letters
1251C - Minimize The Integer
1494B - Berland Crossword
295A - Greg and Array
1433E - Two Round Dances
1612D - X-Magic Pair
41B - Martian Dollar
906C - Party
774F - Pens And Days Of Week
598B - Queries on a String
1303B - National Project
1303D - Fill The Bag